package com.spring.boot.entity;

import cn.hutool.log.Log;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spring.boot.service.SysRoleService;
import com.spring.boot.service.SysUserRoleService;
import com.spring.boot.service.impl.SysMenuServiceImpl;
import com.spring.boot.service.impl.SysRoleMenuServiceImpl;
import com.spring.boot.service.impl.SysRoleServiceImpl;
import com.spring.boot.service.impl.SysUserRoleServiceImpl;
import com.spring.boot.utils.SpringContextUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@TableName("sys_user")//mybatis-plus注解
@Data
public class UserInfo implements UserDetails, Serializable {

 @TableId(type = IdType.AUTO)
    private int id;
    private String username;
    private String password;

  public UserInfo(String username, String password) {
    this.username = username;
    this.password = password;

  }

    public UserInfo() {

    }

    @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
      //这里的角色信息查询会在分页查询的时候用到，不论有没有带参数
//      SysRoleServiceImpl roleService = SpringConttextUtils.getBean("sysRoleServiceImpl",SysRoleServiceImpl.class);
////      SysUserRoleServiceImpl userRoleService = SpringContextUtils.getBean("sysUserRoleServiceImpl",SysUserRoleServiceImpl.class);
////      SysRoleMenuServiceImpl roleMenuService = SpringContextUtils.getBean("sysRoleMenuServiceImpl",SysRoleMenuServiceImpl.class);
////      SysMenuServiceImpl menuService = SpringConextUtils.getBean("sysMenuServiceImpl", SysMenuServiceImpl.class);
      Collection<GrantedAuthority> authorities = new ArrayList<>();
//      List<SysUserRole> userRoleList = userRoleService.getByUserId(this.id);
//      for (SysUserRole userRole : userRoleList) {
//          SysRole role = roleService.getById(userRole.getRoleId());
//          authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getRole()));
//      }
//      System.out.println("authorities=>"+authorities.toString());

    return authorities;
  }

    /**
     * 账户是否过期
     * @return
     */
  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

    /**
     * 账户是否锁定
     * @return
     */
  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

    /**
     * 凭证或密码是否过期
     * @return
     */
  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

    /**
     * 用户是否禁用
     * @return
     */
  @Override
  public boolean isEnabled() {
    return true;
  }
}
